package com.base.library;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.base.library.manager.RealmManager;

import io.realm.Realm;


public abstract class BaseFragment extends Fragment {
    public AppCompatActivity mContext;
    private Fragment mContent;
    private FragmentManager fragmentManager;
    private ViewDataBinding viewDataBinding;
    public RealmManager realm;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        viewDataBinding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
        realm = new RealmManager(Realm.getDefaultInstance());
        View view = viewDataBinding.getRoot();
        mContext = (AppCompatActivity) getActivity();
        fragmentManager = getChildFragmentManager();
        initData(inflater, container, savedInstanceState);
        return view;
    }

    public <T extends ViewDataBinding> T getViewDataBinding() {
        return (T) viewDataBinding;
    }

    public synchronized void init(Fragment from, int layout_id) {
        if (fragmentManager == null) {
            fragmentManager = getChildFragmentManager();
        }
        try {
            if (from == null) {
                return;
            }
            if (!from.isAdded()) {
                mContent = from;
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if (transaction != null) {
                    transaction.add(layout_id, from, from.getClass().getName()).commitAllowingStateLoss();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public synchronized void switchContent(Fragment to, int layout_id) {
        if (fragmentManager == null) {
            fragmentManager = getChildFragmentManager();
        }
        try {
            if (mContent != to) {
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                if (transaction != null) {
                    if (!to.isAdded() && fragmentManager.findFragmentByTag(to.getClass().getName()) == null) { // 先判断是否被add过
                        transaction.hide(mContent).add(layout_id, to, to.getClass().getName()).commitAllowingStateLoss(); // 隐藏当前的fragment，add下一个到Activity中
                    } else {
                        transaction.hide(mContent).show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
                    }
                    mContent = to;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public abstract int getLayoutId();

    public abstract void initData(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle);

    public BaseFragment() {
        super();
    }

    public void showMsg(final String msg) {
        if (isAdded()) {
            getActivity().runOnUiThread(() -> com.base.library.manager.SnackBarManager.showMessage(viewDataBinding.getRoot(), msg));
        }
    }

    public void showMsg(final int resId) {
        if (isAdded()) {
            getActivity().runOnUiThread(() -> com.base.library.manager.SnackBarManager.showMessage(viewDataBinding.getRoot(), resId));
        }
    }

    @Override
    public void onDestroyView() {
        realm.close();
        super.onDestroyView();
    }
}
